package com.itita.weibo.ui;

import java.util.HashMap;
import java.util.List;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.StatusAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;

import weibo4andriod.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


 
public final class ListMyWeibosActivity extends Activity implements IWeiboActivity{
   
	public static final String REF_MYWEIBO="REF_MYWEIBO";//更新微博 
    
    public static final String REF_MYICON="REF_MYICON";//更新头像
    
    public static final String REF_MYWEIBO_MORE="REF_MYWEIBO_MORE";//更多信息
    
    public static final String REF_NEW_MYWEIBO_RESULT="REF_NEW_MYWEIBO_RESULT";//发表微博的结果
   
    public static final String REF_NEW_MYWEIBO_COMM_RESULT="REF_NEW_MYWEIBO_COMM_RESULT";//发表微博的结果
   
    //加载微博中的图片
    public static final String REF_GET_MYWEIBO_IMAGE = "REF_GET_MYWEIBO_IMAGE";
    
    private ListView lv;
    
    private View process;
    
    public int pagesize=5;//每页条数
    
    public int nowpage=1;//页数
    
    protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		WeiboUtil.closeWindowTitle(this);
		//请求当前用户的微博主页 
		HashMap hm=new HashMap();
		hm.put("pagesize", pagesize);
		hm.put("nowpage", nowpage);
		//请求当前用户的微博主页 
		Task tsMine=new Task(Task.TASK_GET_WEIBO_USER,hm);
//	    MainService.addNewTask(tsMine);
		MainService.newTaskToTaskQueneHead(tsMine);
	    
	    
	    MainService.addActivity(this);
	    //测试代码
	     //lv=new ListView(this);
	    this.setContentView(R.layout.myweibo);
	    //获取标题
	    View titleview=this.findViewById(R.id.freelook_title);
	    
	    TextView tv=(TextView)titleview.findViewById(R.id.textView);
	    tv.setVisibility(View.VISIBLE);
	    
//	   final ImageView imageViewTitle = (ImageView)titleview.findViewById(R.id.imageViewTitle);
//	   imageViewTitle.setVisibility(View.GONE);
	    //当前用户的昵称
//	    tv.setText(MainService.currentUser.getName());
	    tv.setText("我的微博");
	    tv.setTextSize((float) 16.0);
		 
		 
	  
	    
	    Button btnHome=(Button)titleview.findViewById(R.id.title_bt_right);
	    btnHome.setBackgroundResource(R.drawable.title_home);
	    btnHome.setOnClickListener(new OnClickListener(){
 	        
			public void onClick(View v) {
 	    	   Intent intent=new Intent(ListMyWeibosActivity.this,HomeActivity.class);
 	    	   ListMyWeibosActivity.this.startActivityForResult(intent, 1);
 	       }
	    }
	    );
	    
	    Button btnBack=(Button)titleview.findViewById(R.id.title_bt_left);
	    btnBack.setBackgroundResource(R.drawable.title_back);
	    
	    btnBack.setOnClickListener(new OnClickListener(){     
			 public void onClick(View arg0) {
		        finish();
			}
	    }
	    );
	    //获取列表对象和进度条对象
	    process=this.findViewById(R.id.progress);
	    lv=(ListView)this.findViewById(R.id.freelook_listview);
	    lv.setClickable(true);
	    	    
	    //对ListView进行事件处理
	    lv.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				WeiboUtil.myToastShow(ListMyWeibosActivity.this, "查看");
				if(arg3==-1){  //更多
					
						nowpage++;
						HashMap hm=new HashMap();
						hm.put("pagesize", pagesize);
						hm.put("nowpage", nowpage);
						Task tsMine=new Task(Task.TASK_GET_MORE_WEIBO_USER,hm);
						MainService.newTaskToTaskQueneHead(tsMine);
					
				}
				 if(arg3==0)
				{//刷新
					HashMap hm=new HashMap();
					hm.put("pagesize", pagesize);
					hm.put("nowpage", nowpage);
			    	Task tsMine=new Task(Task.TASK_GET_WEIBO_USER,hm);
			    	MainService.newTaskToTaskQueneHead(tsMine);
				}
				 if(arg3!=-1 && arg3!=0){

					 
				    Intent it=new Intent(ListMyWeibosActivity.this,  ViewWeiboActivity.class);
				    try{
					    StatusAdapter ma=(StatusAdapter)lv.getAdapter();
					    Status status=(Status)ma.allstatus.get(arg2-1);
					    it.putExtra("status",status);
				    }catch(Exception e){
				    	Log.e("e","error"+e);
				    }				    
				    ListMyWeibosActivity.this.startActivityForResult(it, 0);
				}
				
			}
	    	
	    });
	    //注册上下文菜单
        this.registerForContextMenu(lv);
	}
    
     

	 
	public void init() {
		// TODO Auto-generated method stub
		
	}
	 
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d("keydown", ".................."+keyCode);
		if(keyCode==4)//如果用户按下了返回键
		{
			MainService.promptExitApp(this);
		}
		return true;
	}
	 
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		Log.d("ref", (String)param[0]);
		String flag = (String)param[0];
		if(flag.equals("REF_MYWEIBO"))
	    {
			List<Status> alls=(List<Status>)param[1];
			StatusAdapter ma=new StatusAdapter(this,alls);
			lv.setAdapter(ma);//更新列表
			process.setVisibility(View.GONE);
		}
		else if(flag.equals(REF_MYICON)){ 
			//让数据适配器getView方法重新调用
			((StatusAdapter)lv.getAdapter()).notifyDataSetChanged();
		}
		
		else if(flag.equals(REF_MYWEIBO_MORE)){
			List<Status> alls=(List<Status>)param[1];
			((StatusAdapter)lv.getAdapter()).addMoreData(alls);
		}
		
		else if(flag.equals(REF_NEW_MYWEIBO_RESULT)){
			String result=(String)param[1];
			Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
		}
		
		else if(flag.equals(REF_NEW_MYWEIBO_COMM_RESULT)){   
			String result=(String)param[1];
		    Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
	    }
	    
	    //刷新微博中的图片
		else if(flag.equals(REF_GET_MYWEIBO_IMAGE)){
			//让数据适配器getView方法重新调用
			((StatusAdapter)lv.getAdapter()).notifyDataSetChanged();
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode){
	      case RESULT_OK: /* 取得数据，并显示于画面上 */
	         Bundle bunde = data.getExtras();
	         String newBolgResultMsg = bunde.getString("newBlogResult");
	         Toast.makeText(this, newBolgResultMsg, Toast.LENGTH_SHORT).show();
	         break;
	         
	      default:
	        break;
	    }
	}




	@Override
	public void initControlsAndAddControlsEvent() {
		// TODO Auto-generated method stub
		
	}

}
